package com.itheima.ydd.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.ydd.entity.SysConfig;
import com.itheima.ydd.entity.YddArticle;
import com.itheima.ydd.entity.YddCate;
import com.itheima.ydd.entity.YddUser;
import com.itheima.ydd.mapper.SysConfigMapper;
import com.itheima.ydd.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class SysConfigServiceImpl extends ServiceImpl<SysConfigMapper, SysConfig> implements SysConfigService {

    @Autowired
    private YddUserService yddUserService;
    @Autowired
    private YddCateService yddCateService;
    @Autowired
    private YddArticleService yddArticleService;
    @Autowired
    private YddAdvposService yddAdvposService;
    @Autowired
    private YddTagService yddTagService;
    @Autowired
    private YddAdvimgService yddAdvimgService;
    @Autowired
    private SysUserService sysUserService;

    /**
     * 获取配置信息
     * @return
     */
    @Override
    public Map getSystemConfig() {
        LambdaQueryWrapper<SysConfig> lqw = new LambdaQueryWrapper<>();
        lqw.eq(SysConfig::getTabValue,"system");
        List<SysConfig> list = this.list(lqw);
        Map<String , String> map = new HashMap<>();
        for (SysConfig sysConfig : list) {
            map.put(sysConfig.getAttrKey(),sysConfig.getAttrValue());
        }
        return map;
    }

    /**
     * 主页统计信息
     * @return
     */
    @Override
    public Map getBaseInfo() {
        Map<String , Object> allInfos  = new HashMap<>();
        //获取所有分类数据
        List<YddCate> cates = yddCateService.list();
        //根据cateId去统计对应的文章数量
        List<Map<String , Object>> allCates = new ArrayList<>();
        for (YddCate cate : cates) {
            Map<String , Object> allCate = new HashMap<>();
            allCate.put("id",cate.getId());
            allCate.put("catename",cate.getCatename());
            LambdaQueryWrapper<YddArticle> articleWrapper = new LambdaQueryWrapper<>();
            articleWrapper.eq(YddArticle::getCateid , cate.getId());
            int count = yddArticleService.count(articleWrapper);
            allCate.put("num",count);
            allCates.add(allCate);
        }
        allInfos.put("all_cate",allCates);

        //统计各类总数
        //广告位总数
        int advposNum = yddAdvposService.count();
        allInfos.put("advpos_num",advposNum);
        //标签总数
        int tagNum = yddTagService.count();
        allInfos.put("tag_num",tagNum);
        //文章总数
        int articleNum = yddArticleService.count();
        allInfos.put("article_num",articleNum);
        //分类总数
        int cateNum = yddCateService.count();
        allInfos.put("cate_num",cateNum);
        //广告图总数
        int advimgNum = yddAdvimgService.count();
        allInfos.put("advimg_num",advimgNum);
        //管理员总数
        int adminNum = sysUserService.count();
        allInfos.put("admin_num",adminNum);

        //获取最新注册用户
        Page<YddUser> pageInfo = new Page<>(1,7);
        LambdaQueryWrapper<YddUser> userWrapper = new LambdaQueryWrapper<>();
        userWrapper.orderByDesc(YddUser::getBirthday);
        yddUserService.page(pageInfo,userWrapper);
        List<YddUser> newUser = pageInfo.getRecords();
        allInfos.put("new_user",newUser);

        //统计用户信息，总数 ，男性人数 ， 女性人数
        Map<String , Integer> userMap = new HashMap();
        int total = yddUserService.count(); //总数
        LambdaQueryWrapper<YddUser> userWrapper2 = new LambdaQueryWrapper<>();
        userWrapper2.eq(YddUser::getSex , "1");
        int userMan = yddUserService.count(userWrapper2);//男性
        LambdaQueryWrapper<YddUser> userWrapper3 = new LambdaQueryWrapper<>();
        userWrapper3.eq(YddUser::getSex, "0");
        int userWoman = yddUserService.count(userWrapper3);//女性
        userMap.put("total",total);
        userMap.put("user_man",userMan);
        userMap.put("user_woman",userWoman);

        allInfos.put("user",userMap);

        //热门文章统计
        //首先是推荐文章，然后按照阅读次数排序

        Page<YddArticle> articlePage = new Page<>(1,7);

        LambdaQueryWrapper<YddArticle> articleWrapper = new LambdaQueryWrapper<>();
        articleWrapper.eq(YddArticle::getIshot,"1");
        articleWrapper.orderByDesc(YddArticle::getClick);//按照点击次数降序
        yddArticleService.page(articlePage,articleWrapper);
        List<YddArticle> hotArticle = articlePage.getRecords();
        allInfos.put("hot_article",hotArticle);


        return allInfos;
    }
}
